package com.ruoyi.kiku.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.OssUtils;
import com.ruoyi.kiku.domain.KikuNewsImages;
import com.ruoyi.kiku.mapper.KikuNewsImagesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.kiku.mapper.KikuNewsInfoMapper;
import com.ruoyi.kiku.domain.KikuNewsInfo;
import com.ruoyi.kiku.service.IKikuNewsInfoService;

/**
 * 小鞠新闻Service业务层处理
 *
 * @author TT
 * @date 2025-09-03
 */
@Service
public class KikuNewsInfoServiceImpl implements IKikuNewsInfoService
{
    @Autowired
    private KikuNewsInfoMapper kikuNewsInfoMapper;
    @Autowired
    private KikuNewsImagesMapper kikuNewsImagesMapper;

    /**
     * 查询小鞠新闻
     *
     * @param id 小鞠新闻主键
     * @return 小鞠新闻
     */
    @Override
    public KikuNewsInfo selectKikuNewsInfoById(Long id)
    {
        KikuNewsInfo kikuNewsInfo = kikuNewsInfoMapper.selectKikuNewsInfoById(id);
        List<KikuNewsImages> kikuNewsImages = kikuNewsImagesMapper.selectKikuNewsImagesByNewsId(id);
        kikuNewsInfo.setImages(kikuNewsImages);
        return kikuNewsInfo;
    }

    /**
     * 查询小鞠新闻列表
     *
     * @param kikuNewsInfo 小鞠新闻
     * @return 小鞠新闻
     */
    @Override
    public List<KikuNewsInfo> selectKikuNewsInfoList(KikuNewsInfo kikuNewsInfo)
    {
        return kikuNewsInfoMapper.selectKikuNewsInfoList(kikuNewsInfo);
    }
    /**
     * 查询小鞠新闻列表（官网）
     *
     * @param kikuNewsInfo 小鞠新闻
     * @return 小鞠新闻
     */
    @Override
    public List<KikuNewsInfo> selectKikuNewsInfoList2(KikuNewsInfo kikuNewsInfo)
    {
        return kikuNewsInfoMapper.selectKikuNewsInfoList2(kikuNewsInfo);
    }



    /**
     * 新增小鞠新闻
     *
     * @param kikuNewsInfo 小鞠新闻
     * @return 结果
     */
    @Override
    public int insertKikuNewsInfo(KikuNewsInfo kikuNewsInfo)
    {
        kikuNewsInfo.setCreateTime(DateUtils.getNowDate());
        int i1 = kikuNewsInfoMapper.insertKikuNewsInfo(kikuNewsInfo);
        batchInitImageByImageNum(kikuNewsInfo.getImageNum(), kikuNewsInfo);
        return i1;
    }
    /**
     * 批量插入图片
     * @param imageNum 图片数量
     * @param kikuNewsInfo 小鞠新闻
     */
    public void batchInitImageByImageNum(Integer imageNum, KikuNewsInfo kikuNewsInfo){
        List<KikuNewsInfo> kikuNewsInfos = kikuNewsInfoMapper.selectKikuNewsInfoList(kikuNewsInfo);
        KikuNewsInfo kikuNewsInfo1 = kikuNewsInfos.get(0);
        if (imageNum != null && imageNum > 0){
            for (int i = 0; i < imageNum; i++) {
                KikuNewsImages kikuNewsImages = new KikuNewsImages();
                kikuNewsImages.setNewsId(kikuNewsInfo1.getId());
                kikuNewsImages.setUrl( "");
                kikuNewsImages.setCaption("请修改描述");
                kikuNewsImagesMapper.insertKikuNewsImages(kikuNewsImages);
            }
        }
    }

    /**
     * 修改小鞠新闻
     *
     * @param kikuNewsInfo 小鞠新闻
     * @return 结果
     */
    @Override
    public int updateKikuNewsInfo(KikuNewsInfo kikuNewsInfo)
    {
        int newImageNum = kikuNewsInfo.getImageNum();
        KikuNewsInfo kikuNewsInfo1 = kikuNewsInfoMapper.selectKikuNewsInfoById(kikuNewsInfo.getId());
        int oldImageNum = kikuNewsInfo1.getImageNum();
        if (newImageNum>oldImageNum){
           int addImageNum = newImageNum - oldImageNum;
           batchInitImageByImageNum(addImageNum, kikuNewsInfo1);
        } else if( newImageNum<oldImageNum){
            int deleteImageNum = oldImageNum - newImageNum;
            List<KikuNewsImages> kikuNewsImages = kikuNewsImagesMapper.selectKikuNewsImagesByNewsId(kikuNewsInfo.getId());
            // 从后面的图片开始删除
            for (int i = kikuNewsImages.size()-1; i >= kikuNewsImages.size()-deleteImageNum; i--) {
                kikuNewsImagesMapper.deleteKikuNewsImagesById(kikuNewsImages.get(i).getId());
            }

        }
        kikuNewsInfo.setUpdateTime(DateUtils.getNowDate());
        return kikuNewsInfoMapper.updateKikuNewsInfo(kikuNewsInfo);
    }

    /**
     * 批量删除小鞠新闻
     *
     * @param ids 需要删除的小鞠新闻主键
     * @return 结果
     */
    @Override
    public int deleteKikuNewsInfoByIds(Long[] ids)
    {
        // 删除新闻图片
        for (int i = 0; i < ids.length; i++) {
            KikuNewsInfo kikuNewsInfo = kikuNewsInfoMapper.selectKikuNewsInfoById(ids[i]);
            OssUtils.delete(kikuNewsInfo.getCoverImage());
            List<KikuNewsImages> kikuNewsImages = kikuNewsImagesMapper.selectKikuNewsImagesByNewsId(ids[i]);
            for (KikuNewsImages kikuNewsImage : kikuNewsImages) {
                kikuNewsImagesMapper.deleteKikuNewsImagesById(kikuNewsImage.getId());
            }
        }
        return kikuNewsInfoMapper.deleteKikuNewsInfoByIds(ids);
    }

    /**
     * 删除小鞠新闻信息
     *
     * @param id 小鞠新闻主键
     * @return 结果
     */
    @Override
    public int deleteKikuNewsInfoById(Long id)
    {
        List<KikuNewsImages> kikuNewsImages = kikuNewsImagesMapper.selectKikuNewsImagesByNewsId(id);
        for (KikuNewsImages kikuNewsImage : kikuNewsImages) {
            kikuNewsImagesMapper.deleteKikuNewsImagesById(kikuNewsImage.getId());
        }
        OssUtils.delete(kikuNewsInfoMapper.selectKikuNewsInfoById(id).getCoverImage());
        return kikuNewsInfoMapper.deleteKikuNewsInfoById(id);
    }
}
